﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StockShark.Models;
using StockShark.ViewModels;
using System.Web.Security;

namespace StockShark.Controllers
{
    public class QuotationsController : Controller
    {
        private StockSharkDB db = new StockSharkDB();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Details(int item = 0, string period = "week")
        {
            Instrument instr = db.Instruments.Find(item);
            if (instr == null)
                return HttpNotFound();
            ViewBag.watched = 0;
            if (Request.IsAuthenticated)
            {
                Guid userId = (Guid)Membership.GetUser().ProviderUserKey;
                ViewBag.watched = db.WatchedInstruments.Where(r => r.PaperId == item && r.UserId == userId).Count();
            }
            DateTime start;            
            ViewBag.PeriodSelectList = PeriodList.GetSelectList(period);
            var instrumentQuotations = db.Quotations.Where(r => r.PaperId == item);
            if (period.ToLower().Equals("all"))
            {
                if (instrumentQuotations.Count() > 0)
                    start = instrumentQuotations.Min(r => r.Timestamp);
                else
                    start = DateTime.Today.AddDays(-1);
            }
            else
            {
                try
                {
                    start = PeriodList.GetStartDate(period);
                }
                catch (NotSupportedException e)
                {
                    return HttpNotFound();
                }
            }

            List<Quotation> quots = instrumentQuotations
                .OrderBy(r => r.Timestamp)
                .ToList();
            return View(new InstrumentChartData { Quotations = quots, Instrument = instr, StartDate = start });
        }

        public ActionResult List(string type = "")
        {
            if (type.ToLower().Equals("share"))
            {
                ViewBag.Header = "Akcje - wszystkie";
                ViewBag.Unit = "PLN";
                return View(db.Shares.OrderBy(r => r.Name));
            }
            if (type.ToLower().Equals("sharerg"))
            {
                ViewBag.Header = "Akcje - GPW Rynek Główny";
                ViewBag.Unit = "PLN";
                return View(db.Shares.OrderBy(r => r.Name)
                    .Where(r => r.Market.Equals("GPW Rynek Główny")));
            }
            if (type.ToLower().Equals("sharenc"))
            {
                ViewBag.Header = "Akcje - NewConnect";
                ViewBag.Unit = "PLN";
                return View(db.Shares.OrderBy(r => r.Name)
                    .Where(r => r.Market.Equals("NewConnect")));
            }
            if (type.ToLower().Equals("sharergcon"))
            {
                ViewBag.Header = "Akcje - GPW Rynek Główny (notowania ciągłe)";
                ViewBag.Unit = "PLN";
                return View(db.Shares.OrderBy(r => r.Name)
                    .Where(r => r.Market.Equals("GPW Rynek Główny") && r.Fixing == false));
            }
            if (type.ToLower().Equals("sharergfix"))
            {
                ViewBag.Header = "Akcje - GPW Rynek Główny (notowania jednolite)";
                ViewBag.Unit = "PLN";
                return View(db.Shares.OrderBy(r => r.Name)
                    .Where(r => r.Market.Equals("GPW Rynek Główny") && r.Fixing == true));
            }
            if (type.ToLower().Equals("index"))
            {
                ViewBag.Header = "Indeksy giełdowe";
                ViewBag.Unit = "pkt";
                return View(db.Indexes.OrderBy(r => r.Name));
            }
            if (type.ToLower().Equals("currency"))
            {
                ViewBag.Header = "Waluty (Forex)";
                ViewBag.Unit = "PLN";
                return View(db.Currencies.OrderBy(r => r.Name));
            }
            if (type.ToLower().Equals("material"))
            {
                ViewBag.Header = "Surowce";
                ViewBag.Unit = "PLN";
                return View(db.Materials.OrderBy(r => r.Name));
            }
            return HttpNotFound();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
